require 'open-uri'
require 'rubygems'
require 'sequel'
require 'socket'
require 'rexml/document'
require 'mysql'
include REXML

content = File.new("../config/#{Socket.gethostname}.yml").read
settings = YAML::load content
#$conn = Mysql::new(settings['host'], settings['username'], settings['password'], settings['database'])
DB = Sequel.connect("#{settings['adapter']}://#{settings['username']}:#{settings['password']}@#{settings['host']}/#{settings['database']}")
$basic_info = DB[:basic_info]

$data_lines = IO.readlines("../data/places.tsv")

$current_line = 1

def thread_fetcher(id)
  while $current_line < $data_lines.size
    woeid = $data_lines[$current_line].split(/\t/).first
    $current_line += 1

    url = "http://weather.yahooapis.com/forecastrss?w=#{woeid}&u=c"
    puts "fetch from " + url

    begin
      doc = URI.parse(url).read.gsub(/[&]/, 'and')
#    rescue
#      $current_line_info.insert(:line => $current_line)
#      Thread.exit
    end
    rss = Document.new(doc).root

      unless rss.elements["channel/yweather:location"].nil? then
        city = rss.elements["channel/yweather:location"].attributes["city"]
        lat = rss.elements["channel/item/geo:lat"].text.to_f
        long = rss.elements["channel/item/geo:long"].text.to_f
       $basic_info.insert(:woeid => woeid, :city => city, :lat => lat, :long => long)
#      $conn.query("INSERT INTO basic_info (woeid, city, lat, long) VALUES ('#{woeid}', '#{city}', #{lat}, #{long})")
        
      end

    puts woeid, lat, long, $current_line, id
    
  end
end

def run_fetcher  
  $fetchers = []
  50.times do |i|
    $fetchers[i] = Thread.new { thread_fetcher(i) }
  end

  $fetchers.each { |thread| thread.join  }
end

run_fetcher